<div class="cashDailies index">
    <h2><?php echo __('Caja Diaria  '); ?></h2>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php // echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('user_id', 'Cajero'); ?></th>
            <th><?php echo $this->Paginator->sort('initial_cash', 'Caja Inicial ($)'); ?></th>
            <th><?php echo 'Ventas ($)' ?></th>
            <th><?php echo 'Gastos Extras ($)'; ?></th>
            <th><?php echo 'Total General ($)'; ?></th>
            <th><?php echo 'Fecha'; ?></th>
            <th><?php echo $this->Paginator->sort('Estado'); ?></th>
            <th class="actions"><?php echo __('Opciones'); ?></th>
        </tr>
        <?php foreach ($cashDailies as $cashDaily): ?>
            <tr>
                <td><?php // echo h($cashDaily['CashDaily']['id']); ?>&nbsp;</td>
                <td>
                    <?php echo $this->Html->link($cashDaily['User']['name'], array('controller' => 'users', 'action' => 'view', $cashDaily['User']['id'])); ?>
                </td>
                <td><?php echo h($cashDaily['CashDaily']['initial_cash']); ?>&nbsp;</td>

                <td><?php
//                Acumulo el total sumando cada linea de la venta
                $totalSales = 0;
                foreach ($cashDaily['LineSale'] as $line) {
                    $totalSales += $line['total_line'];
                }

                echo $totalSales;
                    ?>&nbsp;</td>

                <td class="gastos" > <?php
//                Acumulo el total sumando cada linea de la gastos extras
                $totalGastos = 0;
                foreach ($cashDaily['ExtraPayment'] as $line) {
                    $totalGastos += $line['total'];
                }

                echo abs($totalGastos);
                    ?>&nbsp;
                </td>


                <?php if (($cashDaily['CashDaily']['initial_cash'] + $totalSales - abs($totalGastos)) < 0) : ?>

                    <td class="gastos" > 
                    <?php else : ?>
                    <td >
                    <?php endif; ?>

                    <?php echo ($cashDaily['CashDaily']['initial_cash'] + $totalSales - abs($totalGastos)) ?>&nbsp;

                </td>

                <td><?php echo $this->Time->format('d/m/Y H:i:s', $cashDaily['CashDaily']['start_date']) ?>&nbsp;</td>

                <td><?php echo ($cashDaily['CashDaily']['state']) ? 'Cerrada' : 'Abierta'; ?>&nbsp;</td>
                <td class="actions">
                    <?php echo $this->Html->link(__('Cerrar Caja'), array('action' => 'close', $cashDaily['CashDaily']['id'])); ?>
                    <?php echo $this->Form->postLink(__('Borrar'), array('action' => 'delete', $cashDaily['CashDaily']['id']), null, __('¿Esta Seguro de Borrar?', $cashDaily['CashDaily']['id'])); ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
     <?php echo $this->element('footer/paginator') ?>

</div>

<?php echo $this->element('menues/general') ?>

